
const db = require( "../models" );
const logger = require( "../utils/utils.logger" );
const tools = require( "../utils/utils.tools" );

const DAO = require( "../dao/DAO" );
const project = db.project;


// Retrieve all comment from the database.
exports.list = ( req, res ) => {
    const pm = req.body;
    
    DAO.list( project, pm, list => {
        //logger.debug(`${req.method} ${req.baseUrl + req.path} *** 参数：${JSON.stringify(pm)}; 响应：${JSON.stringify(list)}`);
  
        res.sendResult( list )
    } )
};

exports.getAllList = function ( pm, cb ) {
    //获取资历
    DAO.list( project, pm, list => {
         cb( list )
     } )
}
exports.create = ( req, res ) => {
    const pm = req.body;
    // 请求验证
    if ( !pm.startTime ) return res.sendResult( {
        data: '',
        code: 605,
        message: "开始时间不能为空"
    } )
    if ( !pm.name ) return res.sendResult( {
        data: '',
        code: 605,
        message: "项目名称不能为空"
    } )
   
     pm.startTime = tools.timestampToTime( pm.startTime )
    if ( pm.endTime ) {
        pm.endTime = tools.timestampToTime( pm.endTime )
        if ( pm.startTime > pm.endTime ) { 
            return res.sendResult( {
                data: '',
                code: 605,
                message: "项目开始时间不能大于结束时间"
            } )
        }
    }
     DAO.create( project, pm, data => {
         //logger.info(`${req.method} ${req.baseUrl + req.path} *** 参数：${JSON.stringify(pm)}; 响应：${JSON.stringify(data)}`);
         res.sendResult( data )
     } )

};


exports.update = ( req, res ) => {
    const pm = req.body;
    // 请求验证
    if ( !pm.id ) return res.sendResult( {
        data: '',
        code: 605,
        message: "ID不能为空！"
    } )
    pm.startTime ? pm.startTime = tools.timestampToTime( pm.startTime ) : ''
    pm.endTime ? pm.endTime = tools.timestampToTime( pm.endTime ) : ''
    DAO.update( project, pm, {
        id: pm.id
    }, data => {
        res.sendResult( data )
    } )

};
exports.delete = ( req, res ) => {
    const pm = req.body;
    // 请求验证
    if ( !pm.id ) return res.sendResult( {
        data: '',
        code: 605,
        message: "ID不能为空！"
    } )
    DAO.delete( project, {
        id: pm.id
    }, data => {
        res.sendResult( data )
    } )
};
